import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_member/widget/loading_dialog.dart';
import 'package:flutter_member/widget/no_data.dart';
import 'package:flutter_member/widget/universal_card.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';

/// 家长列表
class ParentList extends StatefulWidget {
  @override
  _ParentListState createState() => _ParentListState();
}

class _ParentListState extends State<ParentList> {
  /// 家长列表数据
  List<dynamic> listStuGuardian = [];

  /// 加载中
  bool _loading = true;

  /// 获取所有家长

  Future<void> _getStudentGuardian() async {
    final ParseUser user = await ParseUser.currentUser();
    var query = QueryBuilder(ParseObject("Member"))
      ..whereEqualTo("objectId", user["member"]["objectId"])
      ..includeObject(['guardian', 'guardian.job', 'student']);
    var repGuardian = await query.query();
    if (repGuardian.statusCode == 200 &&
        repGuardian.results != null &&
        repGuardian.results.first["guardian"] != null) {
      setState(() => _loading = false);
      setState(
              () => listStuGuardian = repGuardian.results.map((e) => e).toList());
    } else {
      setState(() => _loading = false);
      print("家长列表为空");
    }
  }

  @override
  void initState() {
    _getStudentGuardian();
//    _getGuardian();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.of(context).size.width;
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0Xff4d80c5),
        title: Text('家長列表'),
      ),
      body: Container(
        width: width,
        color: Colors.white,
        child: Container(
          padding: EdgeInsets.all(20),
          width: width * 0.9,
          child: _body(),
        ),
      ),
    );
  }

  Widget _body() {
    if (_loading == false) {
      if (listStuGuardian.length != 0) {
        return EasyRefresh(
          child: Column(
            children: listStuGuardian
                .map((e) => UniversalCard(
                title: e['guardian']['displayName'], children: [
              _cardItem(title: "工作",value: e['guardian']["job"]["displayName"]),
            ]))
                .toList(),
          ),
        );
      } else {
        return NoDataWidget();
      }
    } else {
      return CLoadingDialog(
        text: "加載中",
      );
    }
  }

  Widget _cardItem({String title, String value}) {
    return Container(
      margin: EdgeInsets.only(top: 8),
      child: Row(
        children: [
          Text(
            "$title",
            style: TextStyle(
                color: Color(0xFFA5A5A5),
                fontSize: ScreenUtil().setSp(14)),
          ),
          Container(
            width: 8,
          ),
          Text("$value",
              style: TextStyle(
                  color: Color(0xFFA5A5A5),
                  fontSize: ScreenUtil().setSp(14)))
        ],
      ),
    );
  }
}
